package com.app.couponsharelife.ui.adapter

import android.graphics.Paint
import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.databinding.ActivityCollectItemBinding
import com.app.couponsharelife.ui.listener.OnLongClickListener

/**
 * Created by guluwa on 2018/11/22.
 */
class CollectListAdapter(val list: MutableList<Any>,
                         private val listener: OnLongClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun setData(data: List<Any>, page: Int) {
        if (page == 2) list.clear()
        list.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            else -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.activity_collect_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else 1
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == 0) {
            (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
        } else {
            val collectItemBean = list[position] as TaobaoGoodBean
            Glide.with((holder as ViewHolder).itemView).asBitmap()
                    .apply(RequestOptions()
                            .placeholder(R.drawable.ic_good_place_holder)
                            .transform(RoundedCorners( 10)))
                    .load(collectItemBean.pict_url)
                    .into(holder.databinding.actOrderItemImg)
            holder.databinding.actCollectItemProName.text = collectItemBean.title
            holder.databinding.actCollectItemDiscountPrice.text = String.format("¥ %s", collectItemBean.final_price)
            holder.databinding.actCollectItemOldPrice.text = String.format("¥ %s", collectItemBean.original_price)
            holder.databinding.actCollectItemOldPrice.paintFlags = holder.databinding.actCollectItemOldPrice.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        }
    }

    inner class ViewHolder(val databinding: ActivityCollectItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(false,adapterPosition, list[adapterPosition])
                }
            }
            databinding.root.setOnLongClickListener {
                if (adapterPosition != -1) {
                    listener.click(true,adapterPosition, list[adapterPosition])
                }
                return@setOnLongClickListener true
            }
        }
    }
}